# -*- coding:utf-8 -*-
__author__ = 'xigu007'
from kivy.properties import StringProperty
from kivy.lang import Builder
from kivy.metrics import dp
from pulldrawerview import PullPanel
from composite_widget.base import RelativeComposite
from recycleview.recycleview import _kivy_1_9_1
from composite_widget.animate import register_animate
from composite_widget.content import LabelContent, ImageContent
from composite_widget.base import Division
from datetime import datetime

Builder.load_string('''
<PullRefresh>:
    canvas:
        Color:
            rgb: 1,1,1
    Division:
        absolute_width: dp(56)
        ImageContent:
            id: image_content
            image_size: None, dp(32)
            source: root.image
    Division:
        orientation: 'vertical'
        Division:
            contents_typesetting: 'anchor'
            LabelContent:
                color: 0.196,0.282,0.365, 1
                font_size:  sp(14)
                text: root.text1
                margin: 0,0,0,1
                anchor: 'left'
                align: 'bottom'
        Division:
            contents_typesetting: 'anchor'
            LabelContent:
                color: 0.196,0.282,0.365, 1
                font_size:  sp(12)
                text: root.text2
                margin: 0,0,1,0
                anchor: 'left'
                align: 'top'
''')

_refresh_ = {'duration':0.6,  'keyframes':[ \
             (0.0,{'rotate':{'angle': 0}}),\
             (1.0,{'rotate':{'angle': 360}}),((0.0,1.0),{'time_func': (0.0,0.0,1.0,1.0)})]}
_arrow_2_up_ = {'duration':0.3,  'keyframes':[ \
             (0.0,{'rotate':{'angle': 0}}),\
             (1.0,{'rotate':{'angle': 180}})]}
_arrow_2_down_ = {'duration':0.3,  'keyframes':[ \
             (0.0,{'rotate':{'angle': 180}}),\
             (1.0,{'rotate':{'angle': 0}})]}

register_animate('refresh', **_refresh_)
register_animate('arrow_2_up', **_arrow_2_up_)
register_animate('arrow_2_down', **_arrow_2_down_)

class PullRefresh(PullPanel, RelativeComposite):
    text1 = StringProperty('')
    text2 = StringProperty('')
    image = StringProperty('./res/refresh_arrow.png')
    def __init__(self,**kwargs):
        self.text1 = '继续下拉..'
        self.text2 = '最近更新:{}'.format(self._get_cur_time())
        super(PullRefresh, self).__init__(**kwargs)
        self.size_hint_y = None
        self.height = dp(86)

    def _get_cur_time(self):
        cur = datetime.now()
        return cur.strftime('%Y-%m-%d %H:%M:%S')

    def on_docked(self, instance, value):
        self.stop_animate_all()
        if value:
            self.text1 = '正在更新..'
            #self.image = 'atlas://res/icon2/refresh_circle'
            self.image = './res/refresh_circle.png'
            self.bind_animate('image_content', 'refresh')
            self.start_animate('image_content', 30)
        else:
            self.text2 = '最近更新:{}'.format(self._get_cur_time())
            image_content = self.ids['image_content']
            if image_content:
                image_content.set_transform(**{'rotate':{'angle':0}})
            #self.image = 'atlas://res/icon2/refresh_arrow'
            self.image = './res/refresh_arrow.png'

    def set_pull_state(self,state):
        #print 'on_pull_state={}'.format(state)
        if self.docked:
            return
        if state == 'above':
            self.text1 = '松开后更新..'
            self.bind_animate('image_content', 'arrow_2_up')
            self.start_animate('image_content')
        else:
            self.text1 = '继续下拉..'
            self.bind_animate('image_content', 'arrow_2_down')
            self.start_animate('image_content')

